package com.fourforfo.fourmall.cart.web;

import com.fourforfo.common.utils.HttpUtils;
import com.fourforfo.fourmall.cart.interceptor.CartInterceptor;
import com.fourforfo.fourmall.cart.service.CartService;
import com.fourforfo.fourmall.cart.vo.CartItemVO;
import com.fourforfo.fourmall.cart.vo.CartVO;
import com.fourforfo.fourmall.cart.vo.UserInfoTO;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.constant.UrlConstant;
import com.xxl.sso.core.user.XxlSsoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: fourmall
 * @description: 处理购物车相关请求
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-08-23 16:46
 **/
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * 获取当前用户的购物商品项
     * @return
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVO> currentUserCartItems(){

        return cartService.getCurrentUserCartItems();
    }

    /**
    * @Description: 偷懒写法，就不用异步请求获取了，懒得改前端请求
    * @Param: [skuId, check:为1表示选中，为0表示未选中]
    * @return: java.lang.String
    * @Author: fourforfo
    * @Date: 2022/8/27
    */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){

        cartService.checkItem(skuId,check);

        return "redirect:http://"+UrlConstant.CART_FOURMALL_COM+"/cart.html";

    }

    /**
     * 修改购物项的数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){

        cartService.countItem(skuId,num);

        return "redirect:http://"+UrlConstant.CART_FOURMALL_COM+"/cart.html";

    }

    /**
     * 修改某个购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){

        cartService.deleteItem(skuId);

        return "redirect:http://"+UrlConstant.CART_FOURMALL_COM+"/cart.html";

    }

    /**
    * @Description: 添加商品到购物车
    * @Param: [skuId：商品skuId, num：商品数量]
    * @return: java.lang.String
    * @Author: fourforfo
    * @Date: 2022/8/24
    */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes){

        CartItemVO cartItemVO = cartService.addCartItemToCart(skuId,num);
        attributes.addAttribute("skuId",cartItemVO.getSkuId());
        return "redirect:http://"+ UrlConstant.CART_FOURMALL_COM+"/success.html";
    }


    /**
     * @Description: 成功添加到购物车
     * @Param: []
     * @return: java.lang.String
     * @Author: fourforfo
     * @Date: 2022/8/23
     */
    @GetMapping("/success.html")
    public String successPage(HttpServletRequest request, @RequestParam("skuId") Long skuId,Model model){


        /**
         * 从request中获取xxlUser信息
         */

        CartItemVO cartItemVO = cartService.getSuccessCartItem(skuId);
        if(cartItemVO!=null){
            model.addAttribute("item",cartItemVO);
        }
        return "success";
    }


    /**
     * @Description: list购物车,浏览器有一个用户标识cookie信息user-key，一个月后过期
     *                  如果第一次使用jd的购物车功能，都会给一个临时的用户身份，
     *                  或者cookie中的用户标识被删除时会重新保存用户标识到cookie中
     * @Param: []
     * @return: java.lang.String
     * @Author: fourforfo
     * @Date: 2022/8/23
     */
    @GetMapping("/cart.html")
    public String cartListPage(HttpServletRequest request, Model model){

        /**
         * 从request中获取xxlUser信息
         */
        UserInfoTO userInfoTO = CartInterceptor.threadLocal.get();

        /**
         * 如果登陆了则获取离线购物车和在线购物车，无需转入参数，service层从ThreadLocal中获取
         * 如果没登录就只获取离线购物车
         */
        CartVO cartVO = cartService.listCartWithUserInfoTO();

        if(cartVO!=null){
            model.addAttribute("cartVO",cartVO);
        }


        return "cartList";
    }



}
